/*
 * Copyright (C) 2008
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.sca4idea.annotators;

import com.intellij.lang.annotation.Annotator;
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotation;
import com.intellij.psi.*;
import com.intellij.openapi.editor.markup.GutterIconRenderer;
import com.intellij.openapi.actionSystem.AnAction;
import com.googlecode.sca4idea.util.SCAAnnotationUtil;
import com.googlecode.sca4idea.util.SCAIcons;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * Annotator for working with various SCA attributes and metadata items within Java files.
 */
public class JavaAnnotator implements Annotator {
    public void annotate(PsiElement psiElement, AnnotationHolder annotationHolder) {
        if (psiElement instanceof PsiField) {
            visitField((PsiField) psiElement, annotationHolder);
        }
    }

    private void visitField(PsiField field, AnnotationHolder annotationHolder) {
        PsiAnnotation refA = SCAAnnotationUtil.getAnnotation(field, SCAAnnotationUtil.REFERENCE_ANNOTATION_CLASS);
        if (refA != null) {
            Annotation fieldInfoA = annotationHolder.createInfoAnnotation(field, null);
            fieldInfoA.setGutterIconRenderer(new ReferenceIconRenderer());
        }

        PsiAnnotation propA = SCAAnnotationUtil.getAnnotation(field, SCAAnnotationUtil.PROPERTY_ANNOTATION_CLASS);
        if (propA != null) {
            Annotation fieldInfoA = annotationHolder.createInfoAnnotation(field, null);
            fieldInfoA.setGutterIconRenderer(new PropertyIconRenderer());
        }
    }

    private static class ReferenceIconRenderer extends GutterIconRenderer {
        public ReferenceIconRenderer() {
        }

        @NotNull
        public Icon getIcon() {
            return SCAIcons.REFERENCE_ICON;
        }

        @Nullable
        public String getTooltipText() {
            return "SCA Reference";
        }

        @Nullable
        public AnAction getClickAction() {
//            return new NavigateAction<S, T>(myImplementable, mySource, myTargets);
            return null;
        }

        public boolean isNavigateAction() {
            return false;
        }
    }

    private static class PropertyIconRenderer extends GutterIconRenderer {

        public PropertyIconRenderer() {
        }

        @NotNull
        public Icon getIcon() {
            return SCAIcons.PROPERTY_ICON;
        }

        @Nullable
        public String getTooltipText() {
            return "SCA Property";
        }

        @Nullable
        public AnAction getClickAction() {
//            return new NavigateAction<S, T>(myImplementable, mySource, myTargets);
            return null;
        }

        public boolean isNavigateAction() {
            return false;
        }
    }
}
